package com.userInfoService.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.userInfoService.entity.PropertyEntity;
import com.userInfoService.entity.UserPojo;
import com.userInfoService.service.EnvironmentService;
import com.userInfoService.service.MqSender;
import com.userInfoService.service.StatusService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@RestController
public class UserInfoController{
	// @RequestBody告诉从body里取值，请求方的Content-Type要设置为application/json，否则报415错误
	// @RequestBody好像不能用多个，如@RequestBody String name,@RequestBody int age
	@RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
	public String addUserInfo(@RequestBody UserPojo userInfo) {
		return userInfo.getName() + ":" + userInfo.getAge();
	}

	// @RequestParam告诉从url里取值
	@RequestMapping(value = "/userInfoStr", method = RequestMethod.GET)
	public String userInfoStr(@RequestParam String name, @RequestParam String age) {
		return "From userInfoService1" + name + ":" + age;
	}
	@RequestMapping(value = "/userInfo", method = RequestMethod.GET)
	public UserPojo userInfo(@RequestParam String name, @RequestParam String age) {
		UserPojo userPojo = new UserPojo();
		userPojo.setAge(age);
		userPojo.setName(name);
		return userPojo;
	}
	
	@Autowired
	private EnvironmentService environmentService;
    @Autowired
    private CounterService counterService;
	
	@RequestMapping(value = "/dbConfigs", method = RequestMethod.GET)
	public List<PropertyEntity> dbConfig() {
		counterService.increment("meter.dbConfigs.count");
		return environmentService.getDBConfig();
	}
	
	@RequestMapping(value = "/otherConfigs", method = RequestMethod.GET)
	public PropertyEntity otherConfigs() {
		return environmentService.getOtherConfigs();
	}
	
	@Autowired
	private StatusService statusService;
	@RequestMapping(value = "/changeMyStatus" ,method = RequestMethod.GET)
	public String changeMyStatus(@RequestParam String myStatus) {
		statusService.setMyStatus(myStatus);
		return "success";
	}
	
	@Autowired
	private MqSender mqSender;
	@RequestMapping(value = "/sendUserPojoToMQ", method = RequestMethod.POST)
	public String sendUserPojoToMQ(@RequestBody UserPojo userPojo) {
		mqSender.sendUserPojo(userPojo);
		return "success";
	}
	
	@RequestMapping(value = "/sendStringToMQ", method = RequestMethod.POST)
	public String sendStringToMQ(@RequestBody UserPojo userPojo) {
		mqSender.sendStr(userPojo);
		return "success";
	}
}
